/**
 * forum demo
 */

layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,table = layui.table
  ,form = layui.form;
  var data = [{
      "id": "1001"
      ,"poster": "赵"
      ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
      ,"content": "为什么花儿这么么红"
      ,"posttime": 20160805
      ,"top": false
    },{
      "id": "1002"
      ,"poster": "钱"
      ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
      ,"content": "喜欢张三，赞赞赞"
      ,"posttime": 20161205
      ,"top": true
    },{
      "id": "1003"
      ,"poster": "孙"
      ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
      ,"content": "明天就要考试了，好紧张，求保佑"
      ,"posttime": 20170405
      ,"top": false
    },{
      "id": "1004"
      ,"poster": "李"
      ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
      ,"content": "希望明天是个好天气"
      ,"posttime": 20171005
      ,"top": false
    },{
      "id": "1005"
      ,"poster": "周"
      ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
      ,"content": "女追男隔层纱，是不是真的"
      ,"posttime": 20180205
      ,"top": false
    },{
      "id": "1006"
      ,"poster": "吴"
      ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
      ,"content": "竟然有人！"
      ,"posttime": 20180512
      ,"top": false
    },{
      "id": "1007"
      ,"poster": "郑"
      ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
      ,"content": "咋了"
      ,"posttime": 20180512
      ,"top": false
    },{
      "id": "1008"
      ,"poster": "王"
      ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
      ,"content": "想知道南昌哪里有好吃的西安肉夹馍"
      ,"posttime": 20180514
      ,"top": false
    }],
      data1 = [{
          "id": "001"
          ,"replyer": "吴"
          ,"cardid": "1002"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "有眼光，我也喜欢张三！"
          ,"replytime": 20161205
        },{
          "id": "002"
          ,"replyer": "郑"
          ,"cardid": "1002"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "同上同上。"
          ,"replytime": 20161205
        },{
          "id": "003"
          ,"replyer": "王"
          ,"cardid": "1003"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "能过一定过，不能过紧张也没用"
          ,"replytime": 20170405
        },{
          "id": "004"
          ,"replyer": "冯"
          ,"cardid": "1001"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "可能因为你。"
          ,"replytime": 20170405
        },{
          "id": "005"
          ,"replyer": "陈"
          ,"cardid": "1003"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "加油加油，看好你"
          ,"replytime": 20170405
        },{
          "id": "006"
          ,"replyer": "褚"
          ,"cardid": "1005"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "纯属喜爱"
          ,"replytime": 20180207
        },{
          "id": "007"
          ,"replyer": "卫"
          ,"cardid": "1005"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "可以试试"
          ,"replytime": 20180207
        },{
          "id": "006"
          ,"replyer": "蒋"
          ,"cardid": "1006"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "是啊是啊，太棒了。"
          ,"replytime": 20180512
        },{
          "id": "007"
          ,"replyer": "沈"
          ,"cardid": "1008"
          ,"avatar": "https://unpkg.com/outeres/demo/avatar/0.png"
          ,"content": "魏家凉皮不错奥。"
          ,"replytime": 20180515
        }];

  //帖子管理
  table.render({
    elem: '#LAY-app-forum-list'
    ,data: data
    // ,url: layui.setter.paths.base + 'json/forum/list.js' //模拟接口
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', width: 100, title: 'ID', sort: true}
      ,{field: 'poster', title: '发帖人'}
      ,{field: 'avatar', title: '头像', width: 100, templet: '#imgTpl'}
      ,{field: 'content', title: '发帖内容'}
      ,{field: 'posttime', title: '发帖时间', sort: true}
      ,{field: 'top', title: '置顶', templet: '#buttonTpl', minWidth: 80, align: 'center'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-forum-list'}
    ]]
    ,page: true
    ,limit: 10
    ,limits: [10, 15, 20, 25, 30]
    ,text: '加载出现异常'
  });
  
  //事件-工具条
  table.on('tool(LAY-app-forum-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此条帖子？', function(index){
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
      var tr = $(obj.tr);

      layer.open({
        type: 2
        ,title: '编辑帖子'
        ,content: '../../../views/app/forum/listform.html'
        ,area: ['550px', '400px']
        ,btn: ['确定', '取消']
        ,resize: false
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-app-forum-submit'
          ,submit = layero.find('iframe').contents().find('#'+ submitID);

          //事件-提交
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field; //获取提交的字段
            
            //提交 Ajax 成功后，静态更新表格中的数据
            //$.ajax({});
            table.reload('LAY-app-forum-list'); //数据刷新
            layer.close(index); //关闭弹层
          });  
          
          submit.trigger('click');
        }
        ,success: function(layero, index){
          
        }
      });
    }
  });

  //回帖管理
  table.render({
    elem: '#LAY-app-forumreply-list'
    ,data: data1
    // ,url: layui.setter.paths.base + 'json/forum/replys.js' //模拟接口
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', width: 100, title: 'ID', sort: true}
      ,{field: 'replyer', title: '回帖人'}
      ,{field: 'cardid', title: '回帖ID', sort: true}
      ,{field: 'avatar', title: '头像', width: 100, templet: '#imgTpl'}
      ,{field: 'content', title: '回帖内容', width: 200}
      ,{field: 'replytime', title: '回帖时间', sort: true}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-forum-replys'}
    ]]
    ,page: true
    ,limit: 10
    ,limits: [10, 15, 20, 25, 30]
    ,text: '对不起，加载出现异常！'
  });
  
  //事件-工具条
  table.on('tool(LAY-app-forumreply-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此条评论？', function(index){
        obj.del();
        layer.close(index);
      });
    } else if(obj.event === 'edit'){
      var tr = $(obj.tr);

      layer.open({
        type: 2
        ,title: '编辑评论'
        ,content: '../../../views/app/forum/replysform.html'
        ,area: ['550px', '350px']
        ,btn: ['确定', '取消']
        ,resize: false
        ,yes: function(index, layero){
          //获取iframe元素的值
          var othis = layero.find('iframe').contents().find("#layuiadmin-form-replys");
          var content = othis.find('textarea[name="content"]').val();
          
          //数据更新
          obj.update({
            content: content
          });
          layer.close(index);
        }
        ,success: function(layero, index){
            
        }

      });
    }
  });
  
  exports('forum', {})
});